package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.CarOrderDetailBean;
import com.incar.chuxing.bean.CheckPositionBean;
import com.incar.chuxing.bean.SmsCodeBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.incar.chuxing.bean.ReturnCarBean;
import com.incar.chuxing.utils.IntentUtils;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReturnCarActivity extends BaseActivity implements View.OnClickListener {

    private Context mContext = this;
    @BindView(R.id.detail_order_toolbar_car)
    RelativeLayout detailOrderToolbarCar;

    @BindView(R.id.edt_beizhu)
    EditText edtBeizhu;
    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.tv_have)
    TextView tvHave;
    @BindView(R.id.tv_return)
    TextView tvReturn;
    @BindView(R.id.activity_about_us)
    LinearLayout activityAboutUs;

    @BindView(R.id.linear_warn)
    LinearLayout linear_warn;
    @BindView(R.id.tv_warnmoney)
    TextView tv_warnmoney;


    private String orderId, carId, parkId;


    @Override
    public int getLayoutId() {
        return R.layout.activity_car_return;
    }

    @Override
    public void initView() {

        titleName.setText("确认还车");


    }

    @Override
    public void initData() {
        super.initData();
        orderId = getIntent().getStringExtra("orderId");
        carId = getIntent().getStringExtra("carId");
        CheckPosition(orderId, carId);
    }


    @OnClick({R.id.title_back, R.id.tv_have, R.id.tv_return})

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.tv_have:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.tv_return:
                dropOffCar(orderId, carId, edtBeizhu.getText().toString());
               /* Intent intent=new Intent(getApplicationContext(),CarParkingFeeActivity.class);
                intent.putExtra(IntentKey.INTENT_ORDER_ID,orderId);
                startActivity(intent);*/
                break;
        }
    }


    private void dropOffCar(final String orderId, String carId, String userRemark) {

        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).returnCar(((MyApplication) getApplication()).getRequestFieldMap(), orderId, carId, "", userRemark).enqueue(new Callback<ReturnCarBean>() {
            @Override
            public void onResponse(Call<ReturnCarBean> call, Response<ReturnCarBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        IntentUtils.toCarParkingFeeActivity(ReturnCarActivity.this, orderId);
                        SharedPreferencesUtils.getInstance(mContext).removeValue(SharedPreferencesKey.CAR_BLE_PSW);
                        SharedPreferencesUtils.getInstance(mContext).removeValue(SharedPreferencesKey.CAR_BLE_IDC);
                        AppManager.getAppManager().finishActivity();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnCarBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.dropOffCarFailure), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void CheckPosition(final String orderId, String carId) {

        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getCheckPosition(((MyApplication) getApplication()).getRequestFieldMap(), orderId, carId).enqueue(new Callback<CheckPositionBean>() {
            @Override
            public void onResponse(Call<CheckPositionBean> call, Response<CheckPositionBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {

                        CheckPositionBean checkPositionBean = response.body();
                        //0外 1内
                        if (checkPositionBean.getAttachment().getIsInFence() == 0) {
                            linear_warn.setVisibility(View.VISIBLE);
                            tv_warnmoney.setText("您在还车范围之外,会产生还车附加费" + checkPositionBean.getAttachment().getReturnMoney() / 100 + "元,是否继续还车？");

                        } else {
                            linear_warn.setVisibility(View.GONE);
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<CheckPositionBean> call, Throwable t) {

                ToastCompat.makeText(mContext, getResources().getString(R.string.dropOffCarFailure), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}




